import pathway as pw
from typing import Dict, Any
from application.db.mysql_db.nsfc.NsfcInfoSectionList import NsfcInfoSectionList


class NsfcInfoSectionListSchema(pw.Schema):
    information_id: str
    section_attr: str = pw.column_definition(default_value='0')
    title_level: int = pw.column_definition(default_value=0)
    marc_code: str = pw.column_definition(default_value='')
    src_text: Dict[str, Any] = pw.column_definition(default_value={})
    dst_text: Dict[str, Any] = pw.column_definition(default_value={})
    media_info: str= pw.column_definition(default_value='')


class NsfcInfoSectionListRead(pw.io.python.ConnectorSubject):

    def run(self):
        select_list = NsfcInfoSectionList.select().order_by(
            NsfcInfoSectionList.information_id.asc(),
            NsfcInfoSectionList.section_order.asc()
        ).dicts()
        for doc in select_list:
            doc = self.transform(doc)
            self.next(**doc)

    def transform(self, transform_data):
        transform_data = {k: v for k, v in transform_data.items()
                          if v is not None and v != {}}

        return transform_data
